package com.example.controller;


import com.example.dao.LeadlogMapper;
import com.example.dao.LeadsMapper;
import com.example.dao.UserMapper;
import com.example.entity.Leadlog;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.entity.Leads;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.io.IOException;

@CrossOrigin
@RestController
@RequestMapping("/lead")

public class LeadController {
    @Autowired
    private LeadsMapper leadsMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LeadlogMapper leadlogMapper;
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @RequiresPermissions("leadsShow")
    @GetMapping(value = "getleadsAll/{page}",produces = "text/html;charset=UTF-8")
    public String getleadAll(@PathVariable int page) throws Exception{

        Map<String, Object> data = new HashedMap();
        data.put("currIndex",(page-1)*10);
        data.put("pageSize",10);
        List<Leads> cList = leadsMapper.selectAll(data);
        for (Leads a:cList) {
            if(a.getCreateTime() != null){
                a.setCreateTimeText(sdf.format(a.getCreateTime()));
            }
            if(a.getGetDispatchTime() != null){
                a.setGetDispatchTimeText(sdf.format(a.getGetDispatchTime()));
            }
            if(a.getLastModifyTime() != null){
                a.setLastModifyTimeText(sdf.format(a.getLastModifyTime()));
            }

        }
        ObjectMapper mapper = new ObjectMapper();
        String JSONString =  mapper.writeValueAsString(cList);
        return JSONString;
    }

    @GetMapping(value = "getmyleadsAll/{page}",produces = "text/html;charset=UTF-8")
    public String getmyleadAll(@PathVariable int page) throws Exception{
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        Map<String, Object> data = new HashedMap();
        data.put("currIndex",(page-1)*10);
        data.put("pageSize",10);
        data.put("username",username);

        List<Leads> cList = leadsMapper.selectMyAll(data);
        for (Leads a:cList) {
            if(a.getCreateTime() != null){
                a.setCreateTimeText(sdf.format(a.getCreateTime()));
            }
            if(a.getGetDispatchTime() != null){
                a.setGetDispatchTimeText(sdf.format(a.getGetDispatchTime()));
            }
            if(a.getLastModifyTime() != null){
                a.setLastModifyTimeText(sdf.format(a.getLastModifyTime()));
            }

        }
        cList.stream().forEach(a->{
            if (a.getOwner_eid() != null && a.getOwner_eid() != ""){
                a.setOwner_eid(userMapper.selectByName(a.getOwner_eid()).getRealname());
            }
        });

        ObjectMapper mapper = new ObjectMapper();
        String JSONString =  mapper.writeValueAsString(cList);
        return JSONString;
    }



    @RequiresPermissions("leadsShow")
    @GetMapping(value = "getleadsCount",produces = "text/html;charset=UTF-8")
    public String getLeadCounts() throws Exception{
        int count = leadsMapper.count();
        ObjectMapper mapper = new ObjectMapper();
        String JSONString =  mapper.writeValueAsString(count);
        return JSONString;
    }

    @RequiresPermissions("leadsShow")
    @GetMapping(value = "getmyleadsCounts",produces = "text/html;charset=UTF-8")
    public String getmyLeadCounts() throws Exception{
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        int count = leadsMapper.mycount(username);
        ObjectMapper mapper = new ObjectMapper();
        String JSONString =  mapper.writeValueAsString(count);
        return JSONString;
    }

    @RequiresPermissions("super")
    @GetMapping(value = "deletelead/{id}",produces = "text/html;charset=UTF-8")
    public String deletelead(@PathVariable int id) throws Exception{
        int result = leadsMapper.deleteByPrimaryKey(id);
        ObjectMapper mapper = new ObjectMapper();
        String JSONString =  mapper.writeValueAsString(result);
        return JSONString;
    }

    @RequiresPermissions("leadsUpdate")
    @PutMapping(produces = "text/html;charset=UTF-8")
    public String updateLead(@RequestBody Leads lead) throws IOException {
        int result = leadsMapper.updateByPrimaryKeySelective(lead);
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(result);
    }

    @RequiresPermissions("leadsAdd")
    @RequestMapping(value="add",produces = "text/html;charset=UTF-8")
    public String addLead(@RequestBody Leads lead) throws IOException {
        int result = leadsMapper.insert(lead);
        ObjectMapper mapper = new ObjectMapper();
        return mapper.writeValueAsString(result);
    }

    @RequiresPermissions("super")
    @GetMapping(value = "getLeadsDropAll/{page}",produces = "text/html;charset=UTF-8")
    public String getClientDropAll(@PathVariable int page) throws Exception{
        Map<String, Object> data = new HashedMap();
        data.put("currIndex",(page-1)*10);
        data.put("pageSize",10);
        List<Leads> cList = leadsMapper.selectLeadDrop(data);
        cList.stream().forEach(a->{
            if (a.getOwner_eid() != null && a.getOwner_eid() != ""){
                a.setOwner_eid(userMapper.selectByName(a.getOwner_eid()).getRealname());
            }
        });
        ObjectMapper mapper = new ObjectMapper();
        String JSONString =  mapper.writeValueAsString(cList);
        return JSONString;
    }

    @RequiresPermissions("super")
    @GetMapping(value = "getLeadsDropCount",produces = "text/html;charset=UTF-8")
    public String getClientDropCounts() throws Exception{
        int count = leadsMapper.dropcount();
        ObjectMapper mapper = new ObjectMapper();
        String JSONString =  mapper.writeValueAsString(count);
        return JSONString;
    }

    @GetMapping(value = "recover/{id}",produces = "text/html;charset=UTF-8")
    public String recoverylead(@PathVariable int id) throws Exception{
        Leads l = new Leads();
        l.setId(id);
        l.setIsDrop("F");
        int count = leadsMapper.updateByPrimaryKeySelective(l);
        ObjectMapper mapper = new ObjectMapper();
        String JSONString =  mapper.writeValueAsString(count);
        return JSONString;
    }

    @RequiresPermissions("super")
    @GetMapping(value = "drop/{id}",produces = "text/html;charset=UTF-8")
    public String drop(@PathVariable int id) throws Exception{
        Leads l = new Leads();
        l.setId(id);
        l.setIsDrop("T");
        int count = leadsMapper.updateByPrimaryKeySelective(l);
        ObjectMapper mapper = new ObjectMapper();
        String JSONString =  mapper.writeValueAsString(count);
        return JSONString;
    }

    @RequiresPermissions("leadsShow")
    @RequestMapping(value = "take",produces = "text/html;charset=UTF-8")
    public String take(@RequestBody Leadlog leadlog) throws Exception{
        int result = 0;
        Leads l = new Leads();
        l.setId(leadlog.getLeadid());
        Subject sub = SecurityUtils.getSubject();
        String eid = sub.getPrincipal().toString();
        l.setOwner_eid(eid);

        Leadlog log = new Leadlog();
        log.setLeadid(leadlog.getLeadid());
        log.setEid(eid);
        log.setLogcontent(leadlog.getLogcontent());
        log.setDatachangeLasttime(new Date());
        int result1 = leadsMapper.updateByPrimaryKeySelective(l);
        int result2 = leadlogMapper.insertSelective(log);
        if(result1 ==1 && result2 ==1){
            result = 1;
        }
        ObjectMapper mapper = new ObjectMapper();
        String JSONString =  mapper.writeValueAsString(result);
        return JSONString;
    }

    @RequiresPermissions("leadsShow")
    @RequestMapping(value = "return",produces = "text/html;charset=UTF-8")
    public String returnlead(@RequestBody Leadlog leadlog) throws Exception{
        int result = 0;
        Leads l = new Leads();
        l.setId(leadlog.getLeadid());
        l.setOwner_eid("");

        Leadlog log = new Leadlog();
        log.setLeadid(leadlog.getLeadid());
        Subject sub = SecurityUtils.getSubject();
        log.setEid(sub.getPrincipal().toString());
        log.setLogcontent(leadlog.getLogcontent());
        log.setDatachangeLasttime(new Date());
        int result1 = leadsMapper.updateByPrimaryKeySelective(l);
        int result2 = leadlogMapper.insertSelective(log);
        if(result1 ==1 && result2 ==1){
            result = 1;
        }
        ObjectMapper mapper = new ObjectMapper();
        String JSONString =  mapper.writeValueAsString(result);
        return JSONString;
    }
}
